package org.misalen.web.database.repository.mysql;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;

import org.hibernate.Query;
import org.hibernate.Session;
import org.misalen.web.database.dao.mysql.impl.BaseMySqlDaoImpl;
import org.misalen.web.database.model.mysql.sys.SysResources;
import org.misalen.web.database.model.mysql.sys.SysResources.SaveType;
import org.misalen.web.database.util.PaginationFrom;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class SysResourcesRepository extends BaseMySqlDaoImpl<SysResources> {

	public List<Map<String, Object>> findResources() {
		// StringBuffer buffer = new StringBuffer();
		// buffer.append("SELECT SysRole.code as code, SysResources.primaryKey
		// as id FROM SysRole ");
		// buffer.append("LEFT JOIN SysRoleResources ON SysRole.primaryKey =
		// SysRoleResources.roleId ");
		// buffer.append("LEFT JOIN SysResources ON SysRoleResources.resourcesId
		// = SysResources.primaryKey ");
		// buffer.append("where SysRole.usingState='available' and
		// SysResources.usingState='available'");
		// Session sess = getCurrentSession();
		// Query query =
		// sess.createSQLQuery(buffer.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
		// List<Map<String, Object>> objects = query.list();
		// for (Map<String, Object> objects2 : objects) {
		// String sql2 = "select * from SysResources where
		// usingState='available' and parentId='" + objects2.get("id")
		// + "'";
		// Query query2 =
		// sess.createSQLQuery(sql2).addEntity(SysResources.class);
		// List<SysResources> resources = query2.list();
		// String url = "";
		// for (SysResources sysResources : resources) {
		// url += sysResources.getResourceUrl() + ",";
		// }
		// if (url.length() > 0) {
		// objects2.put("url", url.substring(0, url.length() - 1));
		// }
		// }
		// return objects;
		return new ArrayList<Map<String, Object>>();

	}

	@Transactional
	@SuppressWarnings("unchecked")
	public List<SysResources> findAdminTree(String adminId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("SELECT SysResources.* FROM SysResources ");
		buffer.append("LEFT JOIN SysRoleResources ON SysResources.primaryKey = SysRoleResources.resourcesId ");
		buffer.append("LEFT JOIN SysAdminRole ON SysAdminRole.roleId = SysRoleResources.roleId ");
		buffer.append("WHERE SysResources.usingState='available' ");
		buffer.append("and (SysResources.saveType='");
		buffer.append(SaveType.menu1.name());
		buffer.append("' or SysResources.saveType='");
		buffer.append(SaveType.menu2.name());
		buffer.append("') ");
		buffer.append("and SysAdminRole.adminId='");
		buffer.append(adminId);
		buffer.append("' order by SysResources.seq desc");
		Session session = openSession();
		Query query = session.createSQLQuery(buffer.toString()).addEntity(SysResources.class);
		List<SysResources> resources = query.list();
		Map<String, SysResources> map = new TreeMap<String, SysResources>();
		for (SysResources sysResources : resources) {
			map.put(sysResources.getPrimaryKey(), sysResources);
		}
		for (Entry<String, SysResources> entry : map.entrySet()) {
			SysResources sysResource = entry.getValue();
			if (sysResource.getSaveType().equals(SaveType.menu2.name())) {
				if (map.containsKey(sysResource.getParentId())) {
					SysResources parent = map.get(sysResource.getParentId());
					if (parent.getChildren() == null) {
						parent.setChildren(new HashSet<SysResources>());
					}
					parent.getChildren().add(sysResource);
				} else {
					SysResources parent = get(SysResources.class, sysResource.getParentId());
					if (parent != null) {
						parent.setChildren(new HashSet<SysResources>());
						parent.getChildren().add(sysResource);
						map.put(parent.getParentId(), parent);
					}
				}
				map.remove(entry.getValue());
			} else if (sysResource.getSaveType().equals(SaveType.menu1.name())) {
				PaginationFrom<SysResources> parent = (PaginationFrom<SysResources>) findPageByHql(
						"from SysResources where parentId=?0", new Object[] { sysResource.getPrimaryKey() }, null);
				Set<SysResources> children = new HashSet<SysResources>();
				for (SysResources sysResources : parent.getList()) {
					children.add(sysResources);
				}
				sysResource.setChildren(children);
			}
		}
		List<SysResources> returnResources = new ArrayList<SysResources>();
		for (SysResources sysResources : map.values()) {
			returnResources.add(sysResources);
		}
		closeSession(session);
		return returnResources;
	}

}